home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gp_mswin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-09  |  25.6 KB  |  1,004 lines

  1. /* Copyright (C) 1992, 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gp_mswin.c */
  20. /*
  21.  * Microsoft Windows 3.n platform support for Ghostscript.
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  25.  */
  26.  
  27. /* Modified for Win32 & Microsoft C/C++ 8.0 32-Bit, 26.Okt.1994 */
  28. /* by Friedrich Nowak                                           */
  29.  
  30. /* Original EXE and GSview specific code removed */
  31. /* DLL version must now be used under MS-Windows */
  32. /* Russell Lang 16 March 1996 */
  33.  
  34. #include "stdio_.h"
  35. #include "string_.h"
  36. #include "memory_.h"
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include "ctype_.h"
  40. #include "dos_.h"
  41. #include <io.h>
  42. #include "malloc_.h"
  43. #include <fcntl.h>
  44. #include <signal.h>
  45. #include "gx.h"
  46. #include "gp.h"
  47. #include "gpcheck.h"
  48. #include "gserrors.h"
  49. #include "gsexit.h"
  50.  
  51. #include "windows_.h"
  52. #include <shellapi.h>
  53. #ifdef __WIN32__
  54. #include <winspool.h>
  55. #endif
  56. #include "gp_mswin.h"
  57. #include "gsdll.h"
  58. /* use longjmp instead of exit when using DLL */
  59. #include <setjmp.h>
  60. extern jmp_buf gsdll_env;
  61.  
  62. /* for redirected I/O */
  63. #include "stream.h"
  64. #include "gxiodev.h"            /* must come after stream.h */
  65.  
  66. /* Library routines not declared in a standard header */
  67. extern char *getenv(P1(const char *));
  68.  
  69. /* Imported from gp_msdos.c */
  70. int gp_file_is_console(P1(FILE *));
  71.  
  72. /* ------ from gnuplot winmain.c plus new stuff ------ */
  73.  
  74. /* limits */
  75. #define MAXSTR 255
  76.  
  77. /* public handles */
  78. HINSTANCE phInstance;
  79. HWND hwndtext = HWND_DESKTOP;    /* would be better to be a real window */
  80.  
  81. const LPSTR szAppName = "Ghostscript";
  82. BOOL is_win32s = FALSE;
  83. char FAR win_prntmp[MAXSTR];    /* filename of PRN temporary file */
  84. private int is_printer(const char *name);
  85. int win_init = 0;        /* flag to know if gp_exit has been called */
  86. int win_exit_status;
  87.  
  88. BOOL CALLBACK _export AbortProc(HDC, int);
  89.  
  90. #ifdef __WIN32__
  91. /* DLL entry point for Borland C++ */
  92. BOOL WINAPI _export
  93. DllEntryPoint(HINSTANCE hInst, DWORD fdwReason, LPVOID lpReserved)
  94. {
  95.     /* Win32s: HIWORD bit 15 is 1 and bit 14 is 0 */
  96.     /* Win95:  HIWORD bit 15 is 1 and bit 14 is 1 */
  97.     /* WinNT:  HIWORD bit 15 is 0 and bit 14 is 0 */
  98.     /* WinNT Shell Update Release is WinNT && LOBYTE(LOWORD) >= 4 */
  99.     DWORD version = GetVersion();
  100.     if ( ((HIWORD(version) & 0x8000) != 0) && ((HIWORD(version) & 0x4000) == 0) )
  101.         is_win32s = TRUE;
  102.  
  103.     phInstance = hInst;
  104.     return TRUE;
  105. }
  106.  
  107. /* DLL entry point for Microsoft Visual C++ */
  108. BOOL WINAPI _export
  109. DllMain(HINSTANCE hInst, DWORD fdwReason, LPVOID lpReserved)
  110. {
  111.     DllEntryPoint(hInst, fdwReason, lpReserved);
  112. }
  113.  
  114.  
  115. #else
  116. int WINAPI _export
  117. LibMain(HINSTANCE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
  118. {
  119.     phInstance = hInstance;
  120.     return 1;
  121. }
  122.  
  123. int WINAPI _export
  124. WEP(int nParam)
  125. {
  126.     return 1;
  127. }
  128. #endif
  129.  
  130.  
  131. BOOL CALLBACK _export
  132. AbortProc(HDC hdcPrn, int code)
  133. {
  134.     process_interrupts();
  135.     if (code == SP_OUTOFDISK)
  136.     return (FALSE);    /* cancel job */
  137.     return(TRUE);
  138. }
  139.   
  140. /* ------ Process message loop ------ */
  141. /*
  142.  * Check messages and interrupts; return true if interrupted.
  143.  * This is called frequently - it must be quick!
  144.  */
  145. int
  146. gp_check_interrupts(void)
  147. {
  148.     return (*pgsdll_callback)(GSDLL_POLL, NULL, 0);
  149. }
  150.  
  151. /* ====== Generic platform procedures ====== */
  152.  
  153. /* ------ Initialization/termination (from gp_itbc.c) ------ */
  154.  
  155. /* Do platform-dependent initialization. */
  156. void
  157. gp_init(void)
  158. {
  159.     win_init = 1;
  160. }
  161.  
  162. /* Do platform-dependent cleanup. */
  163. void
  164. gp_exit(int exit_status, int code)
  165. {
  166.     win_init = 0;
  167.     win_exit_status = exit_status;
  168. }
  169.  
  170. /* Exit the program. */
  171. void
  172. gp_do_exit(int exit_status)
  173. {
  174.     /* Use longjmp since exit would terminate caller */
  175.     /* setjmp code will check gs_exit_status */
  176.     longjmp(gsdll_env, gs_exit_status);
  177. }
  178.  
  179. /* ------ Printer accessing ------ */
  180.   
  181. /* Forward references */
  182. private int gp_printfile(P2(const char *, const char *));
  183.  
  184. /* Open a connection to a printer.  A null file name means use the */
  185. /* standard printer connected to the machine, if any. */
  186. /* Return NULL if the connection could not be opened. */
  187. FILE *
  188. gp_open_printer(char *fname, int binary_mode)
  189. {
  190.     if (is_printer(fname)) 
  191.     {    FILE *pfile;
  192.         pfile = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  193.             win_prntmp, "wb");
  194.         return pfile;
  195.     }
  196.     else
  197.         return fopen(fname, (binary_mode ? "wb" : "w"));
  198. }
  199.  
  200. /* Close the connection to the printer. */
  201. void
  202. gp_close_printer(FILE *pfile, const char *fname)
  203. {
  204.     fclose(pfile);
  205.     if (!is_printer(fname))
  206.         return;        /* a file, not a printer */
  207.  
  208.     gp_printfile(win_prntmp, fname);
  209.     unlink(win_prntmp);
  210. }
  211.  
  212. /* Printer abort procedure and progress/cancel dialog box */
  213. /* Used by Win32 and mswinprn device */
  214.  
  215. HWND hDlgModeless;
  216.  
  217. BOOL CALLBACK _export
  218. PrintAbortProc(HDC hdcPrn, int code)
  219. {
  220.     MSG msg;
  221.     while (hDlgModeless && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  222.     if (hDlgModeless || !IsDialogMessage(hDlgModeless,&msg)) {
  223.             TranslateMessage(&msg);
  224.             DispatchMessage(&msg);
  225.     }
  226.     }
  227.     return(hDlgModeless!=0);
  228. }
  229.  
  230. /* Modeless dialog box - Cancel printing */
  231. BOOL CALLBACK _export
  232. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  233. {
  234.     switch(message) {
  235.     case WM_INITDIALOG:
  236.         SetWindowText(hDlg, szAppName);
  237.         return TRUE;
  238.     case WM_COMMAND:
  239.         switch(LOWORD(wParam)) {
  240.         case IDCANCEL:
  241.             DestroyWindow(hDlg);
  242.             hDlgModeless = 0;
  243.             EndDialog(hDlg, 0);
  244.             return TRUE;
  245.         }
  246.     }
  247.     return FALSE;
  248. }
  249.  
  250. #ifndef __WIN32__
  251.  
  252. /* Windows does not provide API's in the SDK for writing directly to a */
  253. /* printer.  Instead you are supposed to use the Windows printer drivers. */
  254. /* Ghostscript has its own printer drivers, so we need to use some API's */
  255. /* that are documented only in the Device Driver Adaptation Guide */
  256. /* that comes with the DDK.  Prototypes taken from DDK <print.h> */
  257. DECLARE_HANDLE(HPJOB);
  258.  
  259. HPJOB    WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  260. int    WINAPI StartSpoolPage(HPJOB);
  261. int    WINAPI EndSpoolPage(HPJOB);
  262. int    WINAPI WriteSpool(HPJOB, LPSTR, int);
  263. int    WINAPI CloseJob(HPJOB);
  264. int    WINAPI DeleteJob(HPJOB, int);
  265. int    WINAPI WriteDialog(HPJOB, LPSTR, int);
  266. int    WINAPI DeleteSpoolPage(HPJOB);
  267.  
  268. #endif        /* WIN32 */
  269.  
  270. /* Dialog box to select printer port */
  271. BOOL CALLBACK _export
  272. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  273. {
  274. LPSTR entry;
  275.     switch(message) {
  276.     case WM_INITDIALOG:
  277.         entry = (LPSTR)lParam;
  278.         while (*entry) {
  279.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM)entry);
  280.         entry += lstrlen(entry)+1;
  281.         }
  282.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM)0);
  283.         return TRUE;
  284.     case WM_COMMAND:
  285.         switch(LOWORD(wParam)) {
  286.         case SPOOL_PORT:
  287. #ifdef __WIN32__
  288.             if (HIWORD(wParam)
  289. #else
  290.             if (HIWORD(lParam)
  291. #endif
  292.                            == LBN_DBLCLK)
  293.             PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
  294.             return FALSE;
  295.         case IDOK:
  296.             EndDialog(hDlg, 1+(int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
  297.             return TRUE;
  298.         case IDCANCEL:
  299.             EndDialog(hDlg, 0);
  300.             return TRUE;
  301.         }
  302.     }
  303.     return FALSE;
  304. }
  305.  
  306. /* return TRUE if queue looks like a valid printer name */
  307. int
  308. is_spool(const char *queue)
  309. {
  310. char *prefix = "\\\\spool";  /* 7 characters long */
  311. int i;
  312.     for (i=0; i<7; i++) {
  313.     if (prefix[i] == '\\') {
  314.         if ((*queue != '\\') && (*queue != '/'))
  315.             return FALSE;
  316.     }
  317.     else if (tolower(*queue) != prefix[i])
  318.         return FALSE;
  319.     queue++;
  320.     }
  321.     if (*queue && (*queue != '\\') && (*queue != '/'))
  322.     return FALSE;
  323.     return TRUE;
  324. }
  325.  
  326.  
  327. private int 
  328. is_printer(const char *name)
  329. {
  330. char buf[128];
  331.     /* is printer if no name given */
  332.     if (strlen(name) == 0)
  333.     return TRUE;
  334.  
  335.     /*  is printer if name appears in win.ini [ports] section */
  336.     GetProfileString("ports", name, "XYZ", buf, sizeof(buf));
  337.     if ( strlen(name) == 0 || strcmp(buf,"XYZ"))
  338.     return TRUE;
  339.  
  340.     /* is printer if name prefixed by \\spool\ */
  341.     if ( is_spool(name) )
  342.     return TRUE;
  343.  
  344.     return FALSE;
  345. }
  346.  
  347. #ifdef __WIN32__        /* ******** WIN32 ******** */
  348.  
  349. /******************************************************************/
  350. /* Print File to port or queue */
  351. /* port==NULL means prompt for port or queue with dialog box */
  352.  
  353. /* This is messy because Microsoft changed the spooler interface */
  354. /* between Window 3.1 and Windows 95/NT */
  355. /* and didn't provide the spooler interface in Win32s */
  356.  
  357. /* Win95, WinNT: Use OpenPrinter, WritePrinter etc. */
  358. private int gp_printfile_win32(const char *filename, char *port);
  359.  
  360. /* Win32s: Pass to Win16 spooler via gs16spl.exe */ 
  361. private int gp_printfile_gs16spl(const char *filename, const char *port);
  362.  
  363.  
  364. /* valid values for pmport are:
  365.  *   ""
  366.  *      action: WinNT and Win95 use default queue, Win32s prompts for port
  367.  *   "LPT1:" (or other port that appears in win.ini [ports]
  368.  *      action: start gs16spl.exe to print to the port
  369.  *   "\\spool\printer name"
  370.  *      action: send to printer using WritePrinter (WinNT and Win95).
  371.  *      action: translate to port name using win.ini [Devices]
  372.  *              then use gs16spl.exe (Win32s).
  373.  *   "\\spool"
  374.  *      action: prompt for queue name then send to printer using 
  375.  *              WritePrinter (WinNT and Win95).
  376.  *      action: prompt for port then use gs16spl.exe (Win32s).
  377.  *   
  378. /* Print File */
  379. private int
  380. gp_printfile(const char *filename, const char *pmport)
  381. {
  382.     /* treat WinNT and Win95 differently to Win32s */
  383.         if (!is_win32s) {
  384.         if (strlen(pmport)==0) { /* get default printer */
  385.         char buf[256];
  386.         char *p;
  387.         /* WinNT stores default printer in registry and win.ini */
  388.         /* Win95 stores default printer in win.ini */
  389.             GetProfileString("windows", "device", "", buf, sizeof(buf));
  390.         if ( (p = strchr(buf, ',')) != NULL )
  391.             *p = '\0';
  392.             return gp_printfile_win32(filename, buf);
  393.         }
  394.         else if (is_spool(pmport))  {
  395.          if (strlen(pmport) >= 8)
  396.                 return gp_printfile_win32(filename, (char *)pmport+8);
  397.         else
  398.                 return gp_printfile_win32(filename, (char *)NULL);
  399.         }
  400.         else
  401.             return gp_printfile_gs16spl(filename, pmport);
  402.     }
  403.     else {
  404.         /* Win32s */
  405.         if (is_spool(pmport)) {
  406.         if (strlen(pmport) >= 8) {
  407.             /* extract port name from win.ini */
  408.             char driverbuf[256];
  409.             char *output;
  410.             GetProfileString("Devices", pmport+8, "", driverbuf, sizeof(driverbuf));
  411.             strtok(driverbuf, ",");
  412.             output = strtok(NULL, ",");
  413.             return gp_printfile_gs16spl(filename, output);
  414.         }
  415.         else
  416.             return gp_printfile_gs16spl(filename, (char *)NULL);
  417.         }
  418.         else
  419.         return gp_printfile_gs16spl(filename, pmport);
  420.     }
  421. }
  422.  
  423. #define PRINT_BUF_SIZE 16384u
  424. #define PORT_BUF_SIZE 4096
  425.  
  426. char *
  427. get_queues(void)
  428. {
  429. int i;
  430. DWORD count, needed;
  431. PRINTER_INFO_1 *prinfo;
  432. char *enumbuffer;
  433. char *buffer;
  434. char *p;
  435.     /* enumerate all available printers */
  436.     EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 1, NULL, 0, &needed, &count);
  437.     if (needed == 0) {
  438.         /* no printers */
  439.         enumbuffer = malloc(4);
  440.     if (enumbuffer == (char *)NULL)
  441.         return NULL;
  442.         memset(enumbuffer, 0, 4);
  443.         return enumbuffer;
  444.     }
  445.     enumbuffer = malloc(needed);
  446.     if (enumbuffer == (char *)NULL)
  447.     return NULL;
  448.     if (!EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 1, (LPBYTE)enumbuffer, needed, &needed, &count)) {
  449.     char buf[256];
  450.     free(enumbuffer);
  451.     sprintf(buf, "EnumPrinters() failed, error code = %d", GetLastError());
  452.     MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  453.     return NULL;
  454.     }
  455.     prinfo = (PRINTER_INFO_1 *)enumbuffer;
  456.     if ((buffer = malloc(PORT_BUF_SIZE)) == (char *)NULL) {
  457.     free(enumbuffer);
  458.     return NULL;
  459.     }
  460.     /* copy printer names to single buffer */
  461.     p = buffer;
  462.     for (i=0; i<count; i++) {
  463.     if (strlen(prinfo[i].pName) + 1 < (PORT_BUF_SIZE- (p-buffer))) {
  464.         strcpy(p, prinfo[i].pName);
  465.         p += strlen(p) + 1;
  466.     }
  467.     }
  468.     *p = '\0';    /* double null at end */
  469.     free(enumbuffer);
  470.     return buffer;
  471. }
  472.  
  473.  
  474. char *
  475. get_ports(void)
  476. {
  477. char *buffer;
  478. #ifdef __WIN32__
  479.     if (!is_win32s)
  480.         return get_queues();
  481. #endif
  482.  
  483.     if ((buffer = malloc(PORT_BUF_SIZE)) == (char *)NULL)
  484.         return NULL;
  485.     GetProfileString("ports", NULL, "", buffer, PORT_BUF_SIZE);
  486.     return buffer;
  487. }
  488.  
  489. /* return TRUE if queuename available */
  490. /* return FALSE if cancelled or error */
  491. /* if queue non-NULL, use as suggested queue */
  492. BOOL
  493. get_queuename(char *portname, const char *queue)
  494. {
  495. char *buffer;
  496. char *p;
  497. int i, iport;
  498.  
  499.     buffer = get_queues();
  500.     if (buffer == NULL)
  501.     return FALSE;
  502.     if ( (queue == (char *)NULL) || (strlen(queue)==0) ) {
  503.     /* select a queue */
  504.     iport = DialogBoxParam(phInstance, "QueueDlgBox", (HWND)NULL, SpoolDlgProc, (LPARAM)buffer);
  505.     if (!iport) {
  506.         free(buffer);
  507.         return FALSE;
  508.     }
  509.     p = buffer;
  510.     for (i=1; i<iport && strlen(p)!=0; i++)
  511.         p += lstrlen(p)+1;
  512.     /* prepend \\spool\ which is used by Ghostscript to distinguish */
  513.     /* real files from queues */
  514.     strcpy(portname, "\\\\spool\\");
  515.     strcat(portname, p);
  516.     }
  517.     else {
  518.     strcpy(portname, "\\\\spool\\");
  519.     strcat(portname, queue);
  520.     }
  521.  
  522.     free(buffer);
  523.     return TRUE;
  524. }
  525.  
  526. /* return TRUE if portname available */
  527. /* return FALSE if cancelled or error */
  528. /* if port non-NULL, use as suggested port */
  529. BOOL
  530. get_portname(char *portname, const char *port)
  531. {
  532. char *buffer;
  533. char *p;
  534. int i, iport;
  535. char filename[MAXSTR];
  536.     buffer = get_ports();
  537.     if (buffer == NULL)
  538.         return FALSE;
  539.     if ( (port == (char *)NULL) || (strlen(port)==0) ) {
  540.         if (buffer == (char *)NULL)
  541.         return FALSE;
  542.         /* select a port */
  543.         iport = DialogBoxParam(phInstance, "SpoolDlgBox", (HWND)NULL, SpoolDlgProc, (LPARAM)buffer);
  544.         if (!iport) {
  545.             free(buffer);
  546.             return FALSE;
  547.         }
  548.         p = buffer;
  549.         for (i=1; i<iport && strlen(p)!=0; i++)
  550.             p += lstrlen(p)+1;
  551.         strcpy(portname, p);
  552.     }
  553.     else
  554.         strcpy(portname, port);
  555.  
  556.     if (strlen(portname) == 0)
  557.         return FALSE;
  558.     if (strcmp(portname,"FILE:") == 0) {
  559.         OPENFILENAME ofn;
  560.         filename[0] = '\0';
  561.         memset(&ofn, 0, sizeof(OPENFILENAME));
  562.         ofn.lStructSize = sizeof(OPENFILENAME);
  563.         ofn.hwndOwner = (HWND)NULL;
  564.         ofn.lpstrFile = filename;
  565.         ofn.nMaxFile = sizeof(filename);
  566.         ofn.Flags = OFN_PATHMUSTEXIST;
  567.         if (!GetSaveFileName(&ofn)) {
  568.             free(buffer);
  569.             return FALSE;
  570.         }
  571.         strcpy(portname, filename);
  572.     }
  573.     free(buffer);
  574.     return TRUE;
  575. }
  576.  
  577.  
  578. /* True Win32 method, using OpenPrinter, WritePrinter etc. */
  579. private int 
  580. gp_printfile_win32(const char *filename, char *port)
  581. {
  582. DWORD count;
  583. char *buffer;
  584. char portname[MAXSTR];
  585. FILE *f;
  586. HANDLE printer;
  587. DOC_INFO_1 di;
  588. DWORD written;
  589.  
  590.     if (!get_queuename(portname, port))
  591.     return FALSE;
  592.     port = portname + 8;    /* skip over \\spool\ */
  593.  
  594.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  595.         return FALSE;
  596.     
  597.     if ((f = fopen(filename, "rb")) == (FILE *)NULL) {
  598.     free(buffer);
  599.     return FALSE;
  600.     }
  601.  
  602.     /* open a printer */
  603.     if (!OpenPrinter(port, &printer, NULL)) {
  604.     char buf[256];
  605.     sprintf(buf, "OpenPrinter() failed for \042%s\042, error code = %d", port, GetLastError());
  606.     MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  607.     free(buffer);
  608.     return FALSE;
  609.     }
  610.     /* from here until ClosePrinter, should AbortPrinter on error */
  611.  
  612.     di.pDocName = szAppName;
  613.     di.pOutputFile = NULL;
  614.     di.pDatatype = "RAW";  /* for available types see EnumPrintProcessorDatatypes */
  615.     if (!StartDocPrinter(printer, 1, (LPBYTE)&di)) {
  616.     char buf[256];
  617.     sprintf(buf, "StartDocPrinter() failed, error code = %d", GetLastError());
  618.     MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  619.     AbortPrinter(printer);
  620.     free(buffer);
  621.     return FALSE;
  622.     }
  623.    
  624.     /* copy file to printer */
  625.     while ((count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0 ) {
  626.     if (!WritePrinter(printer, (LPVOID)buffer, count, &written)) {
  627.         free(buffer);
  628.         fclose(f);
  629.         AbortPrinter(printer);
  630.         return FALSE;
  631.     }
  632.     }
  633.     fclose(f);
  634.     free(buffer);
  635.  
  636.     if (!EndDocPrinter(printer)) {
  637.     char buf[256];
  638.     sprintf(buf, "EndDocPrinter() failed, error code = %d", GetLastError());
  639.     MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  640.     AbortPrinter(printer);
  641.     return FALSE;
  642.     }
  643.  
  644.     if (!ClosePrinter(printer)) {
  645.     char buf[256];
  646.     sprintf(buf, "ClosePrinter() failed, error code = %d", GetLastError());
  647.     MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  648.     return FALSE;
  649.     }
  650.     return TRUE;
  651. }
  652.  
  653.  
  654. /* Start a 16-bit application gs16spl.exe to print a file */
  655. /* Intended for Win32s where 16-bit spooler functions are not available */
  656. /* and Win32 spooler functions are not implemented. */
  657. int
  658. gp_printfile_gs16spl(const char *filename, const char *port)
  659. {
  660. /* Get printer port list from win.ini */
  661. char portname[MAXSTR];
  662. HINSTANCE hinst;
  663. char command[MAXSTR];
  664. char *p;
  665. HWND hwndspl;
  666.  
  667.     if (!get_portname(portname, port))
  668.         return FALSE;
  669.  
  670.     /* get path to EXE - same as DLL */
  671.     GetModuleFileName(phInstance, command, sizeof(command));
  672.     if ((p = strrchr(command,'\\')) != (char *)NULL)
  673.         p++;
  674.     else
  675.         p = command;
  676.     *p = '\0';
  677.     sprintf(command+strlen(command), "gs16spl.exe %s %s", 
  678.         portname, filename);
  679.  
  680.     hinst = (HINSTANCE)WinExec(command, SW_SHOWNORMAL);
  681.     if (hinst < (HINSTANCE)HINSTANCE_ERROR)
  682.     { char buf[MAXSTR];
  683.         sprintf(buf, "Can't run: %s", command);
  684.         MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  685.         return FALSE;
  686.     }
  687.  
  688.     hwndspl = FindWindow(NULL, "GS Win32s/Win16 spooler");
  689.  
  690.     while (IsWindow(hwndspl)) {
  691.         gp_check_interrupts();
  692.     }
  693.  
  694.     return 0;
  695. }
  696.  
  697.  
  698.  
  699. #else                /* ******** !WIN32 ******** */
  700.  
  701. /* Print File to port */
  702. private int
  703. gp_printfile(const char *filename, const char *pmport)
  704. {
  705. #define PRINT_BUF_SIZE 16384u
  706. char *buffer;
  707. char *portname;
  708. int i, port;
  709. FILE *f;
  710. DLGPROC lpfnSpoolProc;
  711. WORD count;
  712. DLGPROC lpfnCancelProc;
  713. int error = FALSE;
  714. long lsize;
  715. long ldone;
  716. char pcdone[20];
  717. MSG msg;
  718. HPJOB hJob;
  719.  
  720.     if (is_spool(pmport) && (strlen(pmport) >= 8)) {
  721.         /* translate from printer name to port name */
  722.         char driverbuf[256];
  723.         GetProfileString("Devices", pmport+8, "", driverbuf, sizeof(driverbuf));
  724.         strtok(driverbuf, ",");
  725.         pmport = strtok(NULL, ",");
  726.     }
  727.  
  728.     /* get list of ports */
  729.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  730.         return FALSE;
  731.  
  732.     if ( (strlen(pmport)==0) || (strcmp(pmport, "PRN")==0) ) {
  733.         GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
  734.         /* select a port */
  735. #ifdef __WIN32__
  736.         lpfnSpoolProc = (DLGPROC)SpoolDlgProc;
  737. #else
  738. #ifdef __DLL__
  739.         lpfnSpoolProc = (DLGPROC)GetProcAddress(phInstance, "SpoolDlgProc");
  740. #else
  741.         lpfnSpoolProc = (DLGPROC)MakeProcInstance((FARPROC)SpoolDlgProc, phInstance);
  742. #endif
  743. #endif
  744.         port = DialogBoxParam(phInstance, "SpoolDlgBox", (HWND)NULL, lpfnSpoolProc, (LPARAM)buffer);
  745. #if !defined(__WIN32__) && !defined(__DLL__)
  746.         FreeProcInstance((FARPROC)lpfnSpoolProc);
  747. #endif
  748.         if (!port) {
  749.             free(buffer);
  750.             return FALSE;
  751.         }
  752.         portname = buffer;
  753.         for (i=1; i<port && strlen(portname)!=0; i++)
  754.             portname += lstrlen(portname)+1;
  755.     }
  756.     else
  757.         portname = (char *)pmport;    /* Print Manager port name already supplied */
  758.     
  759.     if ((f = fopen(filename, "rb")) == (FILE *)NULL) {
  760.         free(buffer);
  761.         return FALSE;
  762.     }
  763.     fseek(f, 0L, SEEK_END);
  764.     lsize = ftell(f);
  765.     if (lsize <= 0)
  766.         lsize = 1;
  767.     fseek(f, 0L, SEEK_SET);
  768.  
  769.     hJob = OpenJob(portname, filename, (HPJOB)NULL);
  770.     switch ((int)hJob) {
  771.         case SP_APPABORT:
  772.         case SP_ERROR:
  773.         case SP_OUTOFDISK:
  774.         case SP_OUTOFMEMORY:
  775.         case SP_USERABORT:
  776.         fclose(f);
  777.         free(buffer);
  778.         return FALSE;
  779.     }
  780.     if (StartSpoolPage(hJob) < 0)
  781.         error = TRUE;
  782.  
  783. #ifdef __WIN32__
  784.     lpfnCancelProc = (DLGPROC)CancelDlgProc;
  785. #else
  786. #ifdef __DLL__
  787.     lpfnCancelProc = (DLGPROC)GetProcAddress(phInstance, "CancelDlgProc");
  788. #else
  789.     lpfnCancelProc = (DLGPROC)MakeProcInstance((FARPROC)CancelDlgProc, phInstance);
  790. #endif
  791. #endif
  792.     hDlgModeless = CreateDialog(phInstance, "CancelDlgBox", (HWND)NULL, lpfnCancelProc);
  793.     ldone = 0;
  794.  
  795.     while (!error && hDlgModeless 
  796.       && (count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0 ) {
  797.         if (WriteSpool(hJob, buffer, count) < 0)
  798.         error = TRUE;
  799.         ldone += count;
  800.         sprintf(pcdone, "%d %%done", (int)(ldone * 100 / lsize));
  801.         SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  802.         while (PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
  803.         if ((hDlgModeless == 0) || !IsDialogMessage(hDlgModeless, &msg)) {
  804.             TranslateMessage(&msg);
  805.             DispatchMessage(&msg);
  806.           }
  807.           }
  808.       }
  809.     free(buffer);
  810.     fclose(f);
  811.  
  812.     if (!hDlgModeless)
  813.         error=TRUE;
  814.     DestroyWindow(hDlgModeless);
  815.     hDlgModeless = 0;
  816. #if !defined(__WIN32__) && !defined(__DLL__)
  817.     FreeProcInstance((FARPROC)lpfnCancelProc);
  818. #endif
  819.     EndSpoolPage(hJob);
  820.     if (error)
  821.         DeleteJob(hJob, 0);
  822.     else
  823.         CloseJob(hJob);
  824.     return !error;
  825. }
  826.  
  827. #endif                /* ******** (!)WIN32 ******** */
  828.  
  829. /* ------ File naming and accessing ------ */
  830.  
  831. /* Create and open a scratch file with a given name prefix. */
  832. /* Write the actual file name at fname. */
  833. FILE *
  834. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  835. {    char *temp;
  836.     if ( (temp = getenv("TEMP")) == NULL )
  837.         *fname = 0;
  838.     else
  839.     {    strcpy(fname, temp);
  840.         /* Prevent X's in path from being converted by mktemp. */
  841.         for ( temp = fname; *temp; temp++ )
  842.             *temp = tolower(*temp);
  843.         if ( strlen(fname) && (fname[strlen(fname)-1] != '\\') )
  844.             strcat(fname, "\\");
  845.     }
  846.     strcat(fname, prefix);
  847.     strcat(fname, "XXXXXX");
  848.     mktemp(fname);
  849.     return fopen(fname, mode);
  850. }
  851.  
  852. /* Open a file with the given name, as a stream of uninterpreted bytes. */
  853. FILE *
  854. gp_fopen(const char *fname, const char *mode)
  855. {    return fopen(fname, mode);
  856. }
  857.  
  858. /* ====== Substitute for stdio ====== */
  859.  
  860. /* Forward references */
  861. private void win_std_init(void);
  862. private stream_proc_process(win_std_read_process);
  863. private stream_proc_process(win_std_write_process);
  864.  
  865. /* Use a pseudo IODevice to get win_stdio_init called at the right time. */
  866. /* This is bad architecture; we'll fix it later. */
  867. private iodev_proc_init(win_stdio_init);
  868. gx_io_device gs_iodev_wstdio = {
  869.     "wstdio", "Special",
  870.     { win_stdio_init, iodev_no_open_device,
  871.       iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
  872.       iodev_no_delete_file, iodev_no_rename_file,
  873.       iodev_no_file_status, iodev_no_enumerate_files
  874.     }
  875. };
  876.  
  877. /* Do one-time initialization */
  878. private int
  879. win_stdio_init(gx_io_device *iodev, gs_memory_t *mem)
  880. {
  881.     win_std_init();        /* redefine stdin/out/err to our window routines */
  882.     return 0;
  883. }
  884.  
  885. /* Define alternate 'open' routines for our stdin/out/err streams. */
  886.  
  887. extern int iodev_stdin_open(P4(gx_io_device *, const char *, stream **,
  888.                    gs_memory_t *));
  889. private int
  890. win_stdin_open(gx_io_device *iodev, const char *access, stream **ps,
  891.   gs_memory_t *mem)
  892. {    int code = iodev_stdin_open(iodev, access, ps, mem);
  893.     stream *s = *ps;
  894.     if ( code != 1 )
  895.       return code;
  896.     s->procs.process = win_std_read_process;
  897.     s->file = NULL;
  898.     return 0;
  899. }
  900.  
  901. extern int iodev_stdout_open(P4(gx_io_device *, const char *, stream **,
  902.                 gs_memory_t *));
  903. private int
  904. win_stdout_open(gx_io_device *iodev, const char *access, stream **ps,
  905.   gs_memory_t *mem)
  906. {    int code = iodev_stdout_open(iodev, access, ps, mem);
  907.     stream *s = *ps;
  908.     if ( code != 1 )
  909.       return code;
  910.     s->procs.process = win_std_write_process;
  911.     s->file = NULL;
  912.     return 0;
  913. }
  914.  
  915. extern int iodev_stderr_open(P4(gx_io_device *, const char *, stream **,
  916.                 gs_memory_t *));
  917. private int
  918. win_stderr_open(gx_io_device *iodev, const char *access, stream **ps,
  919.   gs_memory_t *mem)
  920. {    int code = iodev_stderr_open(iodev, access, ps, mem);
  921.     stream *s = *ps;
  922.     if ( code != 1 )
  923.       return code;
  924.     s->procs.process = win_std_write_process;
  925.     s->file = NULL;
  926.     return 0;
  927. }
  928.  
  929. /* Patch stdin/out/err to use our windows. */
  930. private void
  931. win_std_init(void)
  932. {
  933.     /* If stdxxx is the console, replace the 'open' routines, */
  934.     /* which haven't gotten called yet. */
  935.  
  936.     if ( gp_file_is_console(gs_stdin) )
  937.       gs_findiodevice((const byte *)"%stdin", 6)->procs.open_device =
  938.         win_stdin_open;
  939.  
  940.     if ( gp_file_is_console(gs_stdout) )
  941.       gs_findiodevice((const byte *)"%stdout", 7)->procs.open_device =
  942.         win_stdout_open;
  943.  
  944.     if ( gp_file_is_console(gs_stderr) )
  945.       gs_findiodevice((const byte *)"%stderr", 7)->procs.open_device =
  946.         win_stderr_open;
  947. }
  948.  
  949. private int
  950. win_std_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  951.   stream_cursor_write *pw, bool last)
  952. {
  953.     int count = pw->limit - pw->ptr;
  954.  
  955.     if ( count == 0 )         /* empty buffer */
  956.         return 1;
  957.  
  958. /* callback to get more input */
  959.     count = (*pgsdll_callback)(GSDLL_STDIN, pw->ptr+1, count);
  960.     if (count == 0) {
  961.         /* EOF */
  962.         /* what should we do? */
  963.         return EOFC;
  964.     }
  965.  
  966.     pw->ptr += count;
  967.     return 1;
  968. }
  969.  
  970.  
  971. private int
  972. win_std_write_process(stream_state *st, stream_cursor_read *pr,
  973.   stream_cursor_write *ignore_pw, bool last)
  974. {    uint count = pr->limit - pr->ptr;
  975.     (*pgsdll_callback)(GSDLL_STDOUT, (char *)(pr->ptr +1), count);
  976.     pr->ptr = pr->limit;
  977.     return 0;
  978. }
  979.  
  980. /* This is used instead of the stdio version. */
  981. /* The declaration must be identical to that in <stdio.h>. */
  982. #if defined(_WIN32) && (defined(_MSC_VER) || defined(_WATCOM_))
  983. int _CRTAPI2
  984. fprintf(FILE *file, const char *fmt, ...)
  985. #else
  986. int _Cdecl _FARFUNC
  987. fprintf(FILE _FAR *file, const char *fmt, ...)
  988. #endif
  989. {
  990. int count;
  991. va_list args;
  992.     va_start(args,fmt);
  993.     if ( gp_file_is_console(file) ) {
  994.         char buf[1024];
  995.         count = vsprintf(buf,fmt,args);
  996.         (*pgsdll_callback)(GSDLL_STDOUT, buf, count);
  997.     }
  998.     else
  999.         count = vfprintf(file, fmt, args);
  1000.     va_end(args);
  1001.     return count;
  1002. }
  1003.  
  1004.